#include "Options.hpp"
#include "CommandLineErrorException.hpp"

Options::Options(int argc, char const * const * argv)
: showHelp_(false)
, showVersion_(false)
, inputFile_()
, outputFile_()
{
	enum KeyState
	{
		NoKey,
		KeyOutput,
	};

	KeyState keyState = NoKey;
	for(int i=1; i<argc; ++i)
	{
		switch(keyState)
		{
		case NoKey:
			if(argv[i][0] == '-')
			{
				if(!strcmp(argv[i], "-o") || !strcmp(argv[i], "--out"))
				{
					keyState = KeyOutput;
				}
				else if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "-?") || !strcmp(argv[i], "--help"))
				{
					inputFile_.clear();
					outputFile_.clear();
					showHelp_ = true;
					return;
				}
				else if(!strcmp(argv[i], "-V") || !strcmp(argv[i], "--version"))
				{
					inputFile_.clear();
					outputFile_.clear();
					showVersion_ = true;
					return;
				}
				else
				{
					throw UnknownKeyException(argv[i]);
				}
			}
			else
			{
				inputFile_ = argv[i];
			}
			break;
		case KeyOutput:
			outputFile_ = argv[i];
			keyState = NoKey;
			break;
		}
	}
}
